package game.web.controller;

import game.web.base.Constants;
import game.web.dao.dto.MyBankDTO;
import game.web.entity.MyBankEntityManager;
import game.web.struts.MyBankForm;
import projectx.core.base.ProjectXSystemException;
import projectx.core.base.ProjectXUserException;
import projectx.transaction.TransactionHandler;
import projectx.web.base.ConstantsProjectX;
import projectx.web.base.ProjectXDbTrxController;
import projectx.web.base.ProjectXForm;
import projectx.web.base.UserContext;

public class MyBankSaveControllor extends ProjectXDbTrxController {
	
	protected MyBankSaveControllor(UserContext uc, ProjectXForm form) {
		super(uc, form);
	}

	@Override
	public Object execute() throws ProjectXUserException,
			ProjectXSystemException {

		debugBegin();

		UserContext uc = getUc();
		String ip = (String) uc.get(ConstantsProjectX.CURRENT_IP);
		String id = (String) uc.get(ConstantsProjectX.USER_ID);
		
		MyBankForm form =(MyBankForm)getForm();
		
		MyBankDTO mybankSaveDTO = new MyBankDTO();
		if(form.getMybank_banktype() > 3 || form.getMybank_banktype() < 0)
			mybankSaveDTO.setBankinfo("未知银行代码");
		else
		{
			if(form.getMybank_bankinfoid() == 1)
			{
				if(form.getMybank_banktype()==1)
					mybankSaveDTO.setBankinfo("招商银行:"+form.getMybank_bankno());
				if(form.getMybank_banktype()==2)
					mybankSaveDTO.setBankinfo("工商银行:"+form.getMybank_bankno());
				if(form.getMybank_banktype()==3)
					mybankSaveDTO.setBankinfo("中国银行:"+form.getMybank_bankno());
				if(form.getMybank_banktype()==4)
					mybankSaveDTO.setBankinfo("建设银行:"+form.getMybank_bankno());
				if(form.getMybank_banktype()==5)
					mybankSaveDTO.setBankinfo("农业银行:"+form.getMybank_bankno());
				if(form.getMybank_banktype()==6)
					mybankSaveDTO.setBankinfo("中国邮政:"+form.getMybank_bankno());
			}
			if(form.getMybank_bankinfoid() == 2)
			{
				mybankSaveDTO.setBankinfo("支付宝账号:"+form.getMybank_zhifubao());
			}
			if(form.getMybank_bankinfoid() == 3)
			{
				mybankSaveDTO.setBankinfo("财付通账号:"+form.getMybank_caifutong());
			}
			
		}
		mybankSaveDTO.setId(id);
		mybankSaveDTO.setBanktype(form.getMybank_banktype());
		mybankSaveDTO.setBankno(form.getMybank_bankno());
		mybankSaveDTO.setZhifubao(form.getMybank_zhifubao());
		mybankSaveDTO.setCaifutong(form.getMybank_caifutong());
		mybankSaveDTO.setCreatetime(getCurrentTimeString());
		mybankSaveDTO.setUpdatetime(getCurrentTimeString());
		mybankSaveDTO.setCreateip(ip);
		mybankSaveDTO.setUpdateip(ip);
		
		MyBankEntityManager.saveOrUpdate(mybankSaveDTO);
				
		//获得银行卡信息
		MyBankDTO mybankDTO = MyBankEntityManager.select(id);
		
		uc.put(Constants.MYBANK_SAVE_BANK_TO_DTO, mybankDTO);

		debugEnd();
		return null;
	}

	public static Object execute(UserContext uc, ProjectXForm form)
			throws ProjectXUserException, ProjectXSystemException {
		return TransactionHandler.call(new MyBankSaveControllor(uc, form));
	}
}
